package xin.nick.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.nick.config.SystemProperties;
import xin.nick.domain.dto.SystemUserCreateDTO;
import xin.nick.domain.dto.SystemUserUpdateDTO;
import xin.nick.domain.query.SystemUserQuery;
import xin.nick.domain.vo.CurrentUserInfoVO;
import xin.nick.domain.vo.SystemUserVO;
import xin.nick.entity.SystemUser;
import xin.nick.manager.SystemUserManager;
import xin.nick.mapper.SystemUserMapper;
import xin.nick.security.SecurityUtil;
import xin.nick.util.QueryUtil;
import xin.nick.util.ResultUtil;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 系统用户信息 服务实现类
 * </p>
 *
 * @author Nick
 * @since 2023-06-23
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class SystemUserService extends ServiceImpl<SystemUserMapper, SystemUser> {

    private final BCryptPasswordEncoder bCryptPasswordEncoder;
    private final SystemProperties systemProperties;
    private final SystemUserManager systemUserManager;

    /**
     * 获取系统用户信息详情
     * @param id Long
     * @return SystemUserVO
     */
    public SystemUserVO getDetail(Long id) {
        Assert.notNull(id, "[id]不可为空");
        SystemUser systemUser = getById(id);
        Assert.notNull(systemUser, "系统用户信息不存在");
        SystemUserVO systemUserVO = BeanUtil.copyProperties(systemUser, SystemUserVO.class);
        return systemUserVO;
    }

    /**
     * 获取系统用户信息列表
     * @param query SystemUserQuery
     * @return List<SystemUserVO>
     */
    public List<SystemUserVO> getList(SystemUserQuery query) {

        SystemUser systemUser = BeanUtil.copyProperties(query, SystemUser.class);
        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<>(systemUser);

        List<SystemUser> systemUserList = list(queryWrapper);

        return BeanUtil.copyToList(systemUserList, SystemUserVO.class);

    }

    /**
     * 获取系统用户信息列表分页
     * @param query SystemUserQuery
     * @return Page<SystemUserVO>
     */
    public Page<SystemUserVO> getListPage(SystemUserQuery query) {

        SystemUser systemUser = BeanUtil.copyProperties(query, SystemUser.class);
        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<>(systemUser);

        Page<SystemUser> page = page(QueryUtil.getQueryPage(), queryWrapper);

        return ResultUtil.convertPage(page, SystemUserVO.class);

    }

    /**
     * 系统用户信息添加
     * @param dto SystemUserCreateDTO
     * @return SystemUserVO
     */
    public SystemUserVO createSystemUser(SystemUserCreateDTO dto) {
        SystemUser systemUser = BeanUtil.copyProperties(dto, SystemUser.class);
        systemUser.setUserId(null);
        save(systemUser);
        return getDetail(systemUser.getUserId());
    }

    /**
     * 系统用户信息更新
     * @param dto SystemUserUpdateDTO
     * @return SystemUserVO
     */
    public SystemUserVO updateSystemUser(SystemUserUpdateDTO dto) {
        Assert.notNull(dto.getUserId(), "[userId]不可为空");
        SystemUser systemUser = BeanUtil.copyProperties(dto, SystemUser.class);
        updateById(systemUser);
        return getDetail(systemUser.getUserId());
    }


    /**
     * 系统用户信息删除
     * @param id Long
     */
    public void deleteById(Long id) {
        Assert.notNull(id, "[id]不可为空");
        removeById(id);
    }


    /**
     * 系统用户信息根据id批量删除
     * @param idList
     */
    public void deleteByIdList(List<Long> idList) {
        if (CollUtil.isEmpty(idList)) {
            return;
        }
        removeByIds(idList);
    }


    /**
     * 更新禁用状态
     *
     * @param id Long
     */
    public void updateDisabled(Long id) {
        Assert.notNull(id, "[id]不可为空");

        Long currentUserId = SecurityUtil.getCurrentUserId();
        Assert.isTrue(!Objects.equals(currentUserId, id), "不可以禁用自己");
        Assert.isTrue(!systemUserManager.isRoot(id), "不可以禁用超级用户");

        SystemUser systemUser = getById(id);
        Assert.notNull(systemUser, "系统用户不存在");

        Boolean disabled = Optional.ofNullable(systemUser.getDisabled()).orElse(false);
        SystemUser systemUserUpdate = new SystemUser();
        systemUserUpdate.setUserId(id);
        systemUserUpdate.setDisabled(!disabled);
        updateById(systemUserUpdate);
    }

    public CurrentUserInfoVO getCurrentUserInfo() {
        Long currentUserId = SecurityUtil.getCurrentUserId();
        SystemUser systemUser = getById(currentUserId);
        return BeanUtil.copyProperties(systemUser, CurrentUserInfoVO.class);
    }

}
